<template>
  <div class="studentList">
    <el-form :inline="true" :model="formInline" class="demo-form-inline" size="small">
        <el-form-item label="姓名">
          <el-input v-model="formInline.name" placeholder="请输入姓名"></el-input>
        </el-form-item>
        <el-form-item >
          <el-button type="primary" size="small" @click="onSubmit">查询</el-button>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" size="small" @click="reset">重置</el-button>
        </el-form-item>
      </el-form>
    <el-table :data="comData" border style="width: 100%">
    <el-table-column prop="name" label="姓名" align="center"></el-table-column>
    <el-table-column prop="age" label="年龄" align="center"></el-table-column>
    <el-table-column prop="sex_text" label="性别" align="center"></el-table-column>
    <el-table-column prop="number" label="学号" align="center"></el-table-column>
    <el-table-column prop="class" label="班级" align="center"></el-table-column>
    <el-table-column prop="state_text" label="状态" align="center"></el-table-column>
    <el-table-column prop="address" label="地址" align="center"></el-table-column>
    <el-table-column prop="phone" label="手机号码" align="center"></el-table-column>
    <el-table-column label="操作" align="center">
       <template slot-scope="scope">
          <el-button type="danger" size="mini" icon="el-icon-delete" @click="del(scope.row)"></el-button>
       </template> 
    </el-table-column>
  </el-table>
  <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="currentPage"
      :page-sizes="[5, 10, 20, 30,50]"
      :page-size="pageSize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total">
    </el-pagination>
  </div>
</template>

<script>
import { getStudentList,delStudentList,findStudentList } from '@/service/api/login';
  export default {
    data() {
      return {
        formInline: {
          name: ''
        },
        tableData: [],
        currentPage:1,
        pageSize:10,
        total:0
      }
    },
    computed:{
      comData(){
        return this.tableData.slice((this.currentPage -1)*this.pageSize,this.currentPage*this.pageSize)
      }
    },
    methods:{
      handleSizeChange(val) {
        this.currentPage = 1
        this.pageSize =val
      },
      handleCurrentChange(val) {
        this.currentPage = val
      },
      async del(row){
       const {data:res} = await delStudentList(row.id)
        if(res.status === 200){
          this.$message({message:res.message,type:'success'})
          this.getDate()
        }
      },
      async onSubmit(){
       const {data:res} = await findStudentList(this.formInline.name)
       if(res.status===200) {
          this.tableData=res.data
          this.total = res.total
          this.tableData.forEach( item =>{
          item.sex == 1 ? item.sex_text='男' : item.sex_text='女'
          item.state == 1  ? (item.state_text='已入学') : item.state == 2 ? (item.state_text='未入学'):(item.state_text='休学中')
        })
      }
      },
      reset(){
        this.formInline.name=''
        this.getDate()
      },
      async getDate() {
        const {data:res} = await getStudentList()
        if(res.status===200) {
          this.tableData=res.data
          this.total = res.total
          this.tableData.forEach( item =>{
          item.sex == 1 ? item.sex_text='男' : item.sex_text='女'
          item.state == 1  ? (item.state_text='已入学') : item.state == 2 ? (item.state_text='未入学'):(item.state_text='休学中')
        })
      }
      }
    },
    created(){
      this.getDate()
    }
  }
</script>

<style lang="scss" scoped>
.studentList{
  .el-pagination{
   text-align: right;
   margin-top: 15px;
  }
  .el-form {
    text-align: left;
  }
}
</style>